fix: gateway returns 401 on hash mismatch - Add InvalidKeyError typed…#437
Merged
greatest0fallt1me merged 2 commits intoJun 25, 2026
Merged
Conversation
… sentinel to apiKeyRepository so callers can distinguish prefix-found-but-hash-wrong from key-not-found without leaking that distinction to clients - Replace naive apiKeys.get(header) in gatewayRoutes with resolveApiKey(): prefix scan + SHA-256 constant-time hash comparison (mirrors the pattern in gatewayApiKeyAuth.ts); both 'not_found' and 'hash_mismatch' map to the same UnauthorizedError(401) so no timing oracle is observable - Add Jest test suite covering: hash mismatch -> 401, no-prefix -> 401, exact match -> passes auth (402 from billing stub), wrong apiId -> 401, revoked key -> 403, identical 401 body for mismatch vs unknown prefix Closes CalloraOrg#421
|
@Biokes Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Closed
4 tasks
Contributor
|
good fix — returning a proper 401 with a typed InvalidKeyError on hash mismatch is the right behavior. merged after a small rebase onto main. 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a request arrived with an API key whose prefix matched a registered key but whose full-key hash did not,
gatewayRoutessurfaced an unhandled exception as a500 Internal Server Error. This fix ensures the correct401 Unauthorizedis returned with no information leaking whether the prefix existed.Root Cause
gatewayRoutes.tsusedapiKeys.get(apiKeyHeader)— a direct Map lookup by the raw key string. It had no concept of prefix-based candidate filtering + hash verification, so any exception in the auth path fell through to the outercatchand produced a 500.Changes
src/repositories/apiKeyRepository.tsInvalidKeyErrorclass (code:INVALID_KEY) thrown when a prefix is found but the hash does not match — makes the failure mode explicit for internal observability without leaking it to clientsverify()to returnnullfor no-prefix-match and throwInvalidKeyErrorfor prefix-found-but-hash-wrongsrc/routes/gatewayRoutes.tsresolveApiKey()helper: prefix scan withtimingSafeEqual+ SHA-256 constant-time hash comparison{ record }on success or{ error: 'not_found' | 'hash_mismatch' }on failureUnauthorizedError(401)so the distinction is never client-observableapiKeys.get(apiKeyHeader)entirelysrc/routes/gatewayRoutes.test.tsgateway route - API key prefix / hash mismatch (bug #421)Security Notes
timingSafeEqualused throughoutnot_foundandhash_mismatchproduce identical status, code, and message — prefix enumeration is not possibleChecklist
InvalidKeyErrortyped for internal observability onlyCloses #421